//
//  SinaUserAccountViewModel.swift
//  SinaWeibo
//
//  Created by 王朝阳 on 2016/10/26.
//  Copyright © 2016年 wangzhaoyang. All rights reserved.
//

import UIKit

class SinaUserAccountViewModel {
    
    //全局访问点
    static let sharedTools: SinaUserAccountViewModel = SinaUserAccountViewModel()
    
    //用户信息对象
    var userAccountModel: SinaUserAccountModel?
    //路径
    let file = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString).appendingPathComponent("userAccount.archiver")
    
    //判断用户是否登录
    var isLogin:Bool{
        return accessToken != nil
    }
    //访问令牌
    //代表用户是否登录
    
    var accessToken:String?{
        if userAccountModel?.expires_Date?.compare(Date()) == ComparisonResult.orderedDescending {
            return userAccountModel?.access_token
        }else {
            return nil
        }
    }
    
    init() {
        userAccountModel = getUserAccountModel()
    }
}
//MARK: -viewModel,帮助控制器请求数据
extension SinaUserAccountViewModel {
    //获取token
    func getUserAccount(code: String, finish:@escaping (Bool)->()) {
        //发送请求
        HMNetworkTools.sharedTools.oauthLoadUserAccount(code: code, success: { (response) in
            //判断 response 是否为 nil
            guard let res = response as?[String: Any] else {
                finish(false)
                return
            }
            //字典转模型
            let userAccountModel = SinaUserAccountModel.yy_model(withJSON: res)
            
            //                print(userAccountModel)
            //判断userAccountModel是否为nil
            guard let model = userAccountModel else {
                finish(false)
                return
            }
            //请求用户信息
            self.getUserInfo(model: model, finish: finish)
            }) { (error) in
                finish(false)
                print("请求失败",error)
        }
    }
    //请求用户信息
    func getUserInfo(model: SinaUserAccountModel, finish:@escaping (Bool)->()){
        //发送请求
        HMNetworkTools.sharedTools.oauthLoadUserInfo(model: model, success: { (response) in
            //判断 response是否为 nil 而且是否可以转化成字典
            guard let res = response as?[String: Any] else {
                finish(false)
                return
            }
            //因为后期需要保存该模型 直接使用一个 model 即可 需要直接手动赋值
            print(res)
            model.screen_name = res["screen_name"] as? String
            model.avatar_large = res["avatar_large"] as? String
            print(model)
            //保存模型
            self.saveUserAccountModel(model: model)
            finish(true)
            }) { (error) in
                finish(false)
                print("请求失败",error)
        }
    }
}
//MARK: - 归档解档的方法
extension SinaUserAccountViewModel {
    //保存用户信息
    func saveUserAccountModel(model: SinaUserAccountModel){
        
        //手动赋值
        userAccountModel = model
        
        NSKeyedArchiver.archiveRootObject(model, toFile: file)
    }
    //获取用户信息对象
    fileprivate func getUserAccountModel()->SinaUserAccountModel? {
        let result = NSKeyedUnarchiver.unarchiveObject(withFile: file) as? SinaUserAccountModel
        return result
    }
}
